﻿CREATE FUNCTION dbo.GenerateUpc()
RETURNS BIGINT
WITH SCHEMABINDING
AS
BEGIN
	DECLARE @nextUpc AS BIGINT 
	SELECT @nextUpc = ISNULL(MAX(Code), 1E5) + 1
	FROM dbo.UPC
	WHERE 
		ISNUMERIC(Code) = 1
		AND CAST(Code AS BIGINT) BETWEEN 1E5 AND 1E6
	RETURN @nextUpc
END